﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace UI
{
    public class RushSweepPage : Page 
    {
        public GameObject rewardItemPrefab;
        public GameObject resultItemPrefab;
        public Transform itemTransform;

        public GameObject confirmBtn;

        List<GameObject> itemList = new List<GameObject>();

        protected override void DoOpen()
        {
            if (options.ContainsKey("type")) {
                if (options["type"] == "0")
                {
                    ProtoBuf.Message.CSRush3Hit msg = new ProtoBuf.Message.CSRush3Hit();
                    NetAPI.SendHttp(ProtoBuf.Message.OpDefine.CSRush3Hit, msg, (pack, b) => {
                        if (b)
                        {
                            ProtoBuf.Message.SCRush3Hit _msg = pack.kBody as ProtoBuf.Message.SCRush3Hit;
                            if (_msg.isValid > 0)
                            {
                                StartCoroutine(ShowResults(_msg));
                            }
                            else
                            {
                                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("rush_sweep_warning"));
                                Close();
                            }
                        }
                    });
                }
                else if (options["type"] == "1")
                {
                    Clear();
                    confirmBtn.SetActive(false);
                    ProtoBuf.Message.CSRushToTop msg = new ProtoBuf.Message.CSRushToTop();
                    NetAPI.SendHttp(ProtoBuf.Message.OpDefine.CSRushToTop, msg, (pack, b) => {
                        if (b)
                        {
                            ProtoBuf.Message.SCRushToTop _msg = pack.kBody as ProtoBuf.Message.SCRushToTop;
                            StartCoroutine(ShowResults(_msg));
                        }
                    });
                }
                else
                {
                    Close();
                }
            }
            else
            {
                Close();
            }
        }

        protected override void DoClose()
        {
            if (options["type"] == "0")
            {
                StaticData.isMazeTraderNeedShow = true;
            }
        }

        IEnumerator ShowResults(ProtoBuf.Message.SCRushToTop msg) {
            int maxFloor = 0;
            for (int i = 0; i < msg.sweepInfo.Count; ++i)
            {
                int floor = msg.sweepInfo[i].floor;
                ProtoBuf.Message.ResultObject gain = msg.sweepInfo[i].gain;
                StaticData.playerData.AddResultObj(gain);
                CreateResultItem(floor, gain);
                if (maxFloor < floor)
                {
                    maxFloor = floor;
                }
                yield return new WaitForSeconds(0.5f);
                if (msg.sweepInfo[i].isSectionEnd > 0)
                {
                    StaticData.playerData.AddResultObj(msg.sweepInfo[i].sectionGain);
                    CreateRewardItem(msg.sweepInfo[i].sectionGain, msg.sweepInfo[i].sectionProp);
                    yield return new WaitForSeconds(0.5f);
                }
            }

            if (maxFloor <= StaticData.rushMaxFloor && msg.sweepInfo.Count > 0)
            {
                AskRushToTop();
            }
            else
            {
                confirmBtn.SetActive(true);
            }

        }

        void AskRushToTop()
        {
            ProtoBuf.Message.CSRushToTop msg = new ProtoBuf.Message.CSRushToTop();
            NetAPI.SendHttp(ProtoBuf.Message.OpDefine.CSRushToTop, msg, (pack, b) => {
                if (b)
                {
                    ProtoBuf.Message.SCRushToTop _msg = pack.kBody as ProtoBuf.Message.SCRushToTop;
                    StartCoroutine(ShowResults(_msg));
                }
            });
        }

        IEnumerator ShowResults(ProtoBuf.Message.SCRush3Hit msg)
        {
            Clear();
            for (int i = 0; i < msg.floor.Count; ++i)
            {
                int floor = msg.floor[i];
                ProtoBuf.Message.ResultObject gain = msg.gain[i];
                bool isWin = msg.isWin[i] > 0 ? true : false;
                StaticData.playerData.AddResultObj(gain);
                CreateResultItem(floor, gain, isWin);
                yield return new WaitForSeconds(0.5f);
            }
        }

        void CreateResultItem(int floor, ProtoBuf.Message.ResultObject gain)
        {
            GameObject obj = Instantiate(resultItemPrefab) as GameObject;
            obj.GetComponent<RushSweepResultItem>().Init(floor, gain);
            CreateItem(obj.GetComponent<RushSweepResultItem>());
        }

        void CreateResultItem(int floor, ProtoBuf.Message.ResultObject gain, bool isWin)
        {
            GameObject obj = Instantiate(resultItemPrefab) as GameObject;
            obj.GetComponent<RushSweepResultItem>().Init(floor, isWin, gain);
            CreateItem(obj.GetComponent<RushSweepResultItem>());
        }

        void CreateRewardItem(ProtoBuf.Message.ResultObject gain, string addition)
        {
            GameObject obj = Instantiate(rewardItemPrefab) as GameObject;
            obj.GetComponent<RushSweepRewardItem>().Init(gain, addition);
            CreateItem(obj.GetComponent<RushSweepRewardItem>());
        }

        void CreateItem(RushSweepItemBase sweepItem)
        {
            sweepItem.transform.SetParent(itemTransform);
            sweepItem.transform.localScale = Vector3.one;
            sweepItem.transform.localEulerAngles = Vector3.zero;

            float currentTotalHeight = 0f;
            foreach (var item in itemList)
            {
                currentTotalHeight += item.GetComponent<RushSweepItemBase>().GetHeight();
            }
            if (currentTotalHeight >= 300f)
            {
                Vector3 p = itemTransform.localPosition;
                p.y += sweepItem.GetHeight();
                TweenPosition.Begin(itemTransform.gameObject, 0.2f, p);
            }
            sweepItem.transform.localPosition = new Vector3(0, -currentTotalHeight, 0);
            TweenAlpha ta = sweepItem.GetComponent<TweenAlpha>();
            ta.ResetToBeginning();
            ta.PlayForward();
            TweenPosition tp = sweepItem.GetComponent<TweenPosition>();
            tp.from = new Vector3(-500, sweepItem.transform.localPosition.y, 0);
            tp.to = new Vector3(0, tp.from.y, 0);
            tp.ResetToBeginning();
            tp.PlayForward();

            sweepItem.gameObject.SetActive(true);

            itemList.Add(sweepItem.gameObject);
        }



        void Clear()
        {
            foreach (var item in itemList)
            {
                GameObject.Destroy(item);
            }
            itemList.Clear();
        }
    }
}
